package com.yang.Hash_;

public class LeetCode383 {
    public static void main(String[] args) {
        String s = "aaabb";
        String b = "aaaba";
        SolutionLeetCode383 solutionLeetCode383 = new SolutionLeetCode383();
        boolean b1 = solutionLeetCode383.canConstruct(s, b);
        System.out.println(b1);
    }
}


class SolutionLeetCode383 {
    public boolean canConstruct(String ransomNote, String magazine) {
        // 如果 杂志字数小于赎金信直接返回false
        if (magazine.length() < ransomNote.length()){
            return false;
        }
        // 定义一个模拟哈希数组
        int[] record = new int[26];

        // for循环统计magazine中各个字符出现次数
        for (char ch : magazine.toCharArray()){
            record[(ch - 'a')] += 1;
        }
        // for循环计算magazine的字符是否够赎金信使用
        for (char ch : ransomNote.toCharArray()){
            record[(ch - 'a')] -= 1;
            if (record[(ch - 'a')] < 0){
                return false;
            }
        }
        return true;
    }
}
